#!/bin/bash

USAGE="
Usage: run_train WORKING_DIR SCRIPTS_DIR ITERS TYPES ATOMS MODEL INSTANCES WEIGHTS\n
    WORKING_DIR      Directory where the main model is located\n
    TEMPLATE_DIR     Directory where the init/learn scripts are located\n
    ITERS            Number of iterations\n
    TYPES            File with the types definition\n
    ATOMS            File with the atoms\n
    ATOMS_TEST       File with the atoms to test\n
    ATOMS_RES        File were to save the testing results\n
    MODEL            Name of the main model\n
    INSTANCES        File where to save the instances version of the corpus\n
    WEIGHTS          Prefix of the files where to save the weights\n
    TEMPLATE_INIT    Name of the template file to use to initilize
    TEMPLATE_LEARN   Name of the template file to use to control the learning
    TEMPLATE_TEST    Name of the template file to use to test
    MODE             1 - Just train\n
                     2 - Train and test\n
                     3 - Init, train and test\n
                     5 - Test test cases
    ATOMS_TESTS      TEST sets to test (we suppose .atoms ending and .label)\n
    ID               Identifier of experiment\n
    PROID            Identifier for the process running\n
"

RND=$RANDOM
if [ $# -gt 17 ]
then
    RND=${18}
fi


if [ $# -lt 15 -o $# -gt 18 ]
then 
        echo "Missing arguments $#"
        echo -n -e $USAGE
        exit 1;
fi

WORKING_DIR=$1
TEMPLATE_DIR=$2
ITERS=$3
TYPES=`echo $4 | sed 's/\//\\\\\//g'`
ATOMS=`echo $5 | sed 's/\//\\\\\//g'`
ATOMS_TEST=`echo $6 | sed 's/\//\\\\\//g'`
ATOMS_RES=`echo $7 | sed 's/\//\\\\\//g'`
MODEL=`echo $8 | sed 's/\//\\\\\//g'`
MODELS=`echo $9 | sed 's/\//\\\\\//g'`
INSTANCES=`echo ${10} | sed 's/\//\\\\\//g'`
WEIGHTS=`echo ${11} | sed 's/\//\\\\\//g' | sed "s/PROID/${RND}/g"`
TEMPLATE_INI=`echo ${12} | sed 's/\//\\\\\//g'`
TEMPLATE_LEARN=`echo ${13} | sed 's/\//\\\\\//g'`
TEMPLATE_TEST=`echo ${14} | sed 's/\//\\\\\//g'`
DATE=`date "+%m%d%y_%H%M%S"`
if [ $# -gt 16 ]
then
    DATE=${17}
fi

LABEL="${ATOMS_RES}.${DATE}"

TAR_ULABEL="${7}.${DATE}.tar"
ULABEL="${7}.${DATE}"
UWEIGHTS=`echo ${11} | sed "s/PROID/${RND}/g"`
TAR_UWEIGHTS="${11}.${DATE}.tar"
TAR_MODELS="${9}.${DATE}.tar"

MODE=${15}
ATOMS_TESTS=`echo ${16} | sed 's/::/ /g'`

EPOCHS_PER_ITER=1

THEBEAST="nice /group/project/conllx/task2008/app/thebeast/bin/linux/thebeast"

TEMPLATE_INIT="${TEMPLATE_DIR}/${TEMPLATE_INI}"
TEMPLATE_LEARN="${TEMPLATE_DIR}/${TEMPLATE_LEARN}"
TEMPLATE_TEST="${TEMPLATE_DIR}/${TEMPLATE_TEST}"


# Begining
set -x


# Initializing temporal files
echo "ID: ${DATE}"

if [ $MODE -eq 5 ]
then
    TMP_TEST="__tmp_test.${RND}.pml"
    TAR_ULABEL="${7}.${DATE}.test.tar"
    ITERS=`echo ${ITERS} | sed 's/::/ /g'`
    for iter in $ITERS
    do
        WEIGHTS=`echo "${11}.${iter}" | sed 's/\//\\\\\//g' | sed "s/PROID/${RND}/g"`
        UWEIGHTS_FIX=`echo "$UWEIGHTS.${iter}" | sed 's/^\///' | sed "s/PROID/${RND}/g"`
        TOTAL=`echo "$UWEIGHTS_FIX" | sed 's/[^/]//g' | wc -m`
        NUM=$(($TOTAL-1))
        tar --overwrite -xzvf ${TAR_MODELS}.gz
        pushd "${DATE}_DIR"
        tar --overwrite --strip $NUM -xzvf $TAR_UWEIGHTS.gz "$UWEIGHTS_FIX"
        UWEIGHTS_FIX=`echo "$UWEIGHTS_FIX" | sed 's/.*\///' | sed "s/PROID/${RND}/g"`
        cp $UWEIGHTS_FIX $UWEIGHTS.${iter}
        for tt in $ATOMS_TESTS
        do
            echo "Labeling $tt"
            ATOMS_TEST=`echo "$tt.atoms" | sed 's/\//\\\\\//g'`
            LABEL=`echo "${tt}.${DATE}.${iter}" | sed 's/\//\\\\\//g'`
            ULABEL=`echo "${tt}.${DATE}.${iter}"`
            # Test the last one
            sed "s/TYPES/${TYPES}/g" ${TEMPLATE_TEST} |\
                sed "s/MODEL/${MODEL}/g" |\
                sed "s/WEIGHTS/${WEIGHTS}/g" |\
                sed "s/ATOMS/${ATOMS_TEST}/g" |\
                sed "s/LABEL/${LABEL}.label/g" |\
                sed "s/INSTANCES/${INSTANCES}/g" > $TMP_TEST
            cat $TMP_TEST
            $THEBEAST $TMP_TEST
            ../bin/beast2conll08.py "${ULABEL}.label" > "${ULABEL}.conll08"
            if [ -a $TAR_ULABEL ]
            then 
                tar -rf $TAR_ULABEL "${ULABEL}.label"
            else
                tar -cf $TAR_ULABEL "${ULABEL}.label"
            fi 
            tar -rf $TAR_ULABEL "${ULABEL}.conll08"
            rm "${ULABEL}.label"
        done
    done
    rm $TMP_TEST
    exit 0;
fi






if [ $MODE -gt 0 ]
then 
    if [ $MODE -eq 2 ]
    then   
        tar --overwrite -xzvf ${TAR_MODELS}.gz
        pushd "${DATE}_DIR"
    else
        mkdir "${DATE}_DIR"
        cp ${WORKING_DIR}/*.pml "${DATE}_DIR"
        cp ${WORKING_DIR}/global.atoms "${DATE}_DIR"
        tar -cvf ${TAR_MODELS} "${DATE}_DIR"
        gzip ${TAR_MODELS}
        pushd "${DATE}_DIR"
    fi
    TMP_LEARN="__tmp_learn.${RND}.pml"
    # Learn
    sed "s/TYPES/${TYPES}/g" ${TEMPLATE_LEARN} |\
        sed "s/MODEL/${MODEL}/g" |\
        sed "s/WEIGHTS/${WEIGHTS}/g" |\
        sed "s/ATOMS/${ATOMS}/g" |\
        sed "s/EPOCHS/${EPOCHS_PER_ITER}/g" |\
        sed "s/INSTANCES/${INSTANCES}/g" > $TMP_LEARN
    if [ $MODE -gt 2 ]
    then 
        TMP_INIT="__tmp_init.${RND}.pml"
        # Initialize the corpus
        sed "s/TYPES/${TYPES}/g" ${TEMPLATE_INIT} |\
            sed "s/MODEL/${MODEL}/g" |\
            sed "s/WEIGHTS/${WEIGHTS}/g" |\
            sed "s/ATOMS/${ATOMS}/g" |\
            sed "s/INSTANCES/${INSTANCES}/g" > $TMP_INIT
        $THEBEAST $TMP_INIT
        rm $TMP_INIT
    fi

    for iter in `seq ${ITERS}`
    do  

        if [ $MODE -eq 3 ]
        then
            $THEBEAST $TMP_LEARN
            cp $UWEIGHTS "$UWEIGHTS.${iter}"
            if [ -a $TAR_UWEIGHTS ]
            then
                tar -rf $TAR_UWEIGHTS "$UWEIGHTS.${iter}"
            else
                tar -cf $TAR_UWEIGHTS "$UWEIGHTS.${iter}"
            fi
        fi
        if [ $MODE -eq 2 ]
        then 
            WEIGHTS=`echo "${11}.${iter}" | sed 's/\//\\\\\//g' | sed "s/PROID/${RND}/g"`
            UWEIGHTS_FIX=`echo "$UWEIGHTS.${iter}" | sed 's/^\///' | sed "s/PROID/${RND}/g"`
            TOTAL=`echo "$UWEIGHTS_FIX" | sed 's/[^/]//g' | wc -m`
            NUM=$(($TOTAL-1))
            tar --overwrite --strip $NUM -xzvf $TAR_UWEIGHTS.gz "$UWEIGHTS_FIX"
            UWEIGHTS_FIX=`echo "$UWEIGHTS_FIX" | sed 's/.*\///' | sed "s/PROID/${RND}/g"`
            cp $UWEIGHTS_FIX $UWEIGHTS.${iter}
        fi

       

        TMP_TEST="__tmp_test.${RND}.pml"
        # Test the last one
        sed "s/TYPES/${TYPES}/g" ${TEMPLATE_TEST} |\
            sed "s/MODEL/${MODEL}/g" |\
            sed "s/WEIGHTS/${WEIGHTS}/g" |\
            sed "s/ATOMS/${ATOMS_TEST}/g" |\
            sed "s/LABEL/${LABEL}.label/g" |\
            sed "s/INSTANCES/${INSTANCES}/g" > $TMP_TEST

        if [ $MODE -gt 1 ]
        then
            $THEBEAST $TMP_TEST
            cp "${ULABEL}.label" "${ULABEL}.${iter}.label"
            ../bin/beast2conll08.py "${ULABEL}.${iter}.label" > "${ULABEL}.${iter}.conll08"
            if [ -a $TAR_ULABEL ]
            then 
                tar -rf $TAR_ULABEL "${ULABEL}.${iter}.label"
            else
                tar -cf $TAR_ULABEL "${ULABEL}.${iter}.label"
            fi 
            tar -rf $TAR_ULABEL "${ULABEL}.${iter}.conll08"
            rm "${ULABEL}.${iter}.label"
        fi
    done
    if [ $MODE -eq 3 ]
    then 
        gzip $TAR_UWEIGHTS
        rm $TMP_LEARN
    fi
fi

if [ $MODE -gt 2 ]
then
    gzip $TAR_ULABEL
    rm $TMP_TEST
fi

if [ $MODE -eq 2 ]
then
    echo "Please gzip the file $TAR_ULABEL by yourself"
fi


popd
rm -rf "${DATE}_DIR"






